package com.usermanage.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpCookie;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.usermanage.bean.Admin;
import com.usermanage.service.AdminService;
import com.usermanage.service.impl.AdminServiceImpl;



/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/AdminServlet")
@MultipartConfig()//表示当前Servlet支持文件上传
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取前台点击动作
		String action=request.getParameter("action");
		if (action.equals("register")) {
			register(request, response);
			
		}else if (action.equals("login")) {
			login(request, response);
		}else if (action.equals("queryByAccount")) {
			//获取前台账号查重
			String account=request.getParameter("account");
			System.out.println("需要查找重复的账号："+account);
			/*将用户信息传到Service逻辑接口判断返回结果*/
			AdminService as=new AdminServiceImpl();
			String res=as.queryRepeatByAccount(account);
			//将结果响应给前端
			response.getWriter().write(res);
		}
	}
	/**
	 * 用户登陆
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, IOException {
		/*验证码获取*/
		//获取登陆页面输入验证码
		String logincode=request.getParameter("code1");
		System.out.println("登陆页面输入验证码为"+logincode);
		//获取后台生成验证码
		HttpSession session=request.getSession();
		String code=(String) session.getAttribute("code");
		System.out.println("后台生成验证码为"+code);
		
		String res="";
		if (code.equalsIgnoreCase(logincode)) {
			/*获取前台登陆信息*/
			
			Admin a=new Admin();
			a.setAccount(request.getParameter("account"));
			a.setPassword(request.getParameter("password"));
			/*判断有没有记住我*/
			String[] remember=request.getParameterValues("remember");
			boolean flag=false;
			if (remember!=null) {
				flag=true;
			}
			/*将用户信息传到Service逻辑接口判断返回结果*/
			AdminService as=new AdminServiceImpl();
			Admin admin=as.queryAdminByAccount(a);
			System.out.println(admin.getAccount());
			System.out.println(a.getAccount());
			if (admin.getAccount().equals(a.getAccount())) {
				
				if (admin.getPassword().equals(a.getPassword())) {
					if (flag) {
						Cookie c1=new Cookie("account", URLEncoder.encode(admin.getAccount(), "utf-8"));
						Cookie c2=new Cookie("password", admin.getPassword());
						Cookie c3=new Cookie("name", URLEncoder.encode(admin.getName(), "utf-8"));
						session.setAttribute("account", a.getAccount());
						session.setAttribute("password", a.getPassword());
						response.addCookie(c1);
						response.addCookie(c2);
						response.addCookie(c3);
						System.out.println("添加cookie成功");
					}
					res="<script language='javascript' type='text/javascript'>alert('登陆成功');window.location='UserServlet?action=show';</script>";
				}else {
					System.out.println("密码错误");
					res="<script language='javascript' type='text/javascript'>alert('密码错误');window.location='login.jsp';</script>";
				}
				
				
			}else {
				System.out.println("账号错误");
				res="<script language='javascript' type='text/javascript'>alert('没有查询到此账号');window.location='login.jsp';</script>";
			}
			
		}else {
			System.out.println("验证码错误");
			res="<script language='javascript' type='text/javascript'>alert('验证码错误');window.location.href='login.jsp?backurl='+window.location.href;</script>";
		}
		response.getWriter().write(res);
	}
	/**
	 * 用户注册
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void register(HttpServletRequest request, HttpServletResponse response) throws IOException {
		/*验证码获取*/
		//获取注册页面输入验证码
		String regcode=request.getParameter("code1");
		System.out.println("注册页面输入验证码为"+regcode);
		//获取后台生成验证码
		HttpSession session=request.getSession();
		String code=(String) session.getAttribute("code");
		System.out.println("后台生成验证码为"+code);
		/*获取前台用户信息*/
		Admin a=new Admin();
		a.setAccount(request.getParameter("account"));
		a.setPassword(request.getParameter("password"));
		a.setName(request.getParameter("name"));
		System.out.println(a);
		String rep="";
		if (code.equalsIgnoreCase(regcode)) {

			//创建业务逻辑访问接口
			AdminService as=new AdminServiceImpl();
			int res=as.addAdmin(a);
			if (res>0) {
				rep="<script language='javascript' type='text/javascript'>alert('注册成功');window.location='login.jsp';</script>";
			}else {
				rep="<script language='javascript' type='text/javascript'>alert('注册失败');window.location.href='register.jsp?backurl='+window.location.href;</script>";
			}
			
		}else {
			rep="<script language='javascript' type='text/javascript'>alert('验证码错误');window.location.href='register.jsp?backurl='+window.location.href;</script>";
		}
		response.getWriter().write(rep);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
